package com.common.sdk.net.connect.http.interceptor;

import com.sohu.trafficstatistics.util.TrafficStatisticUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by chengjiangsang116630 on 2018/3/5.
 */

public class MobileStatisticInterceptor extends ExceptionCatchedInterceptor {

    @Override
    protected Response realIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        long length = -1;
        ResponseBody body = response.body();
        if (body != null) {
            length = body.contentLength();
        }
        if (request != null && request.url() != null) {
            String url = request.url().toString();
            TrafficStatisticUtil.recordData(url, length);
        }

        return response;
    }
}
